//
//  ChatRecordView.swift
//  聊天记录
//
//  Created by 小柠檬🍋 on 2025/9/7.
//

import SwiftUI

struct ChatRecordView: View {
    @StateObject var viewModel = ChatRecordViewModel()
    
    var body: some View {
        ZStack{
            if !viewModel.status.isEmpty {
                HomeEmptyView(text1: viewModel.status)
            } else {
                ZStack{
                    ScrollView {
                        //需要嵌套一层LazyVStack，不然会产生间距，而且大小还会随机
                        LazyVStack(spacing: 10) {
                            ForEach(viewModel.items,id: \.id) { item in
                                ChatRecordItemView(bean: item, recordModel: viewModel)
                            }
                            LoadMoreFooter(refreshing: $viewModel.footerRefreshing, action: {
                                viewModel.getData()
                            }) {
                                LoadMoreView(noMore: viewModel.noMore, footerRefreshing: viewModel.footerRefreshing, hideNoMore: true)
                            }
                            .noMore(viewModel.noMore)
                            .preload(offset: 10)
                        }
                    }
                    .enableRefresh()
                    .scrollIndicators(.hidden) // 隐藏滚动条
                    .padding(.leading, 20)
                    .padding(.trailing,15)
                    
                    HStack{
                        Spacer()
                        VStack{
                            Spacer()
                            Image(.homeIconAdd)
                                .resizable()
                                .frame(width: 20, height: 20)
                                .padding(9)
                                .background(themeManager.themeColor)
                                .clipShape(Circle())
                                .contentShape(Rectangle())
                                .onTapGesture {
                                    pathManager.jump(type: JumpType.AI_ROLE)
                                }
                        }.padding(.bottom,30)
                            .padding(.trailing,20)
                    }.background(.clear)
                }
            }}
        .onAppear{
            viewModel.getData()
            //            if !viewModel.isInit {
            //                viewModel.getData()
            //                viewModel.isInit = true
            //            }
        }
    }
}

//#Preview {
//    ChatListView()
//}

//定义预览提供者
struct ChatRecordView_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: ChatRecordView())
    }
}
